<?php

namespace app\admin\controller;



use app\common\controller\SysAction;



class Department extends SysAction

{

    protected $modelClass = '\app\common\model\Department';

    

    public function initialize()

    {

        parent::initialize();

    }

    

    public function index()
    {  

        $school_id = input('param.id');

        $where = [];

        if (input('get.search')){

            $where[] = ['title|url', 'like', '%'.input('get.search').'%'];

        }
        if($school_id){
            $where['school_id'] = $school_id;
        }
       
        $dataList = $this->cModel->where($where)->order($order)->paginate('', false, page_param());

        $this->assign('dataList', $dataList);
        $this->assign('school_id',$school_id);
        return $this->fetch();

    }


      /**
     * @Title: create
     * @Description: todo(添加院系)
     * @return mixed
     * @author 若谷
     * @date 2019年6月6日
     * @throws
     */
    public function create()
    {    
        $school_id = input('param.school_id');
         if (request()->isPost()){
            $data = input('post.');
            $data['createtime'] = time();
            $result = $this->validate($data, 'app\common\validate\\'.CONTROLLER_NAME.'.add');
            if(true !== $result){
                return ajax_return($result);
            }
            $result = $this->cModel->allowField(true)->save($data);
            if ($result){
                return ajax_return(lang('action_success'), url('index?id='.$school_id));
            }else{
                return ajax_return($this->cModel->getError());
            }
        }else{
            $this->assign('school_id',$school_id);
            return $this->fetch('create');
        }
    }

    /**
     * @Title: create
     * @Description: todo(编辑院系)
     * @return mixed
     * @author 若谷
     * @date 2019年6月10日
     * @throws
     */
    public function edit($id)
    {   
        $school_id = input('param.school_id');
        if (request()->isPost()){
            $data = input('post.');
            $data['createtime'] = time();
            if (count($data) == 2){
                foreach ($data as $k =>$v){
                    $fv = $k!='id' ? $k : '';
                }
                $result = $this->validate($data, 'app\common\validate\\'.CONTROLLER_NAME.'.'.$fv);
                if(true !== $result){
                    return ajax_return($result);
                }
                $result = $this->cModel->allowField(true)->save($data, $data['id']);
            }else{
                $result = $this->validate($data, 'app\common\validate\\'.CONTROLLER_NAME.'.edit');
                if(true !== $result){
                    return ajax_return($result);
                }
                $result = $this->cModel->allowField(true)->save($data, $data['id']);
            }
            if ($result){
                return ajax_return(lang('action_success'), url('index?id='.$school_id));
            }else{
                return ajax_return($this->cModel->getError());
            }
        }else{
            $data = $this->cModel->get($id);
            $this->assign('school_id',$school_id);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    /**
     * @Title: delete
     * @Description: todo(软删除院系)
     * @return mixed
     * @author 若谷
     * @date 2019年6月10日
     * @throws
     */
    public function delete()
    {
        if (request()->isPost()){
            $id = input('id');
            if (isset($id) && !empty($id)){
                Db::startTrans();
                try{
                    $id_arr = explode(',', $id);

                    $result = A::destroy($id_arr);              //删除主表数据
                    $result1 = AgentWallet::destroy($id_arr);       //删除钱包
                    $result2 = AgentInfo::destroy($id_arr);         //删除加盟商信息表

                    // 提交事务
                    if ($result && $result1){
                        Db::commit();
                        return ajax_return(lang('action_success'), url('index'));
                    }else{
                        return ajax_return($this->cModel->getError());
                    }
                } catch (\Exception $e) {
                    // 回滚事务
                    Db::rollback();
                    return ajax_return($e->getMessage());
                }
            }
        }
    }


}